3 * Blocks and bans object
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12 define ( 'EB_RANGE_ONLY', 4 );
16 * All the functions in this class assume the object is either explicitly
17 * loaded or filled. It is not load-on-demand. There are no accessors.
19 * To use delete(), you only need to fill $mAddress
20 * Globals used: $wgBlockCache, $wgAutoblockExpiry
22 * @todo This could be used everywhere, but it isn't.
27 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
28 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
30 function Block( $address = '', $user = '', $by = 0, $reason = '',
31 $timestamp = '' , $auto = 0, $expiry = '' )
33 $this->mAddress
= $address;
36 $this->mReason
= $reason;
37 $this->mTimestamp
= wfTimestamp(TS_MW
,$timestamp);
39 if( empty( $expiry ) ) {
40 $this->mExpiry
= $expiry;
42 $this->mExpiry
= wfTimestamp( TS_MW
, $expiry );
45 $this->mForUpdate
= false;
46 $this->initialiseRange();
49 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
52 $ban->load( $address, $user, $killExpired );
58 $mAddress = $mReason = $mTimestamp = '';
63 * Get a ban from the DB, with either the given address or the given username
65 function load( $address = '', $user = 0, $killExpired = true )
67 global $wgDBmysql4, $wgAntiLockFlags;
68 $fname = 'Block::load';
69 wfDebug( "Block::load: '$address', '$user', $killExpired\n" );
73 if ( $this->forUpdate() ) {
74 $db =& wfGetDB( DB_MASTER
);
75 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
78 $options = 'FOR UPDATE';
81 $db =& wfGetDB( DB_SLAVE
);
84 $ipblocks = $db->tableName( 'ipblocks' );
86 if ( 0 == $user && $address=='' ) {
87 $sql = "SELECT * from $ipblocks $options";
88 } elseif ($address=="") {
89 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
90 } elseif ($user=="") {
91 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
92 } elseif ( $options=='' && $wgDBmysql4 ) {
93 # If there are no optiones (e.g. FOR UPDATE), use a UNION
94 # so that the query can make efficient use of indices
95 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
96 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
98 # If there are options, a UNION can not be used, use one
99 # SELECT instead. Will do a full table scan.
100 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
101 "' OR ipb_user={$user}) $options";
104 $res = $db->query( $sql, $fname );
105 if ( 0 == $db->numRows( $res ) ) {
106 # User is not blocked
110 $row = $db->fetchObject( $res );
111 $this->initFromRow( $row );
113 if ( $killExpired ) {
114 # If requested, delete expired rows
116 $killed = $this->deleteIfExpired();
118 $row = $db->fetchObject( $res );
120 $this->initFromRow( $row );
123 } while ( $killed && $row );
125 # If there were any left after the killing finished, return true
136 $db->freeResult( $res );
140 function initFromRow( $row )
142 $this->mAddress
= $row->ipb_address
;
143 $this->mReason
= $row->ipb_reason
;
144 $this->mTimestamp
= wfTimestamp(TS_MW
,$row->ipb_timestamp
);
145 $this->mUser
= $row->ipb_user
;
146 $this->mBy
= $row->ipb_by
;
147 $this->mAuto
= $row->ipb_auto
;
148 $this->mId
= $row->ipb_id
;
149 $this->mExpiry
= $row->ipb_expiry ?
150 wfTimestamp(TS_MW
,$row->ipb_expiry
) :
153 $this->initialiseRange();
156 function initialiseRange()
158 if ( $this->mUser
== 0 ) {
159 $rangeParts = explode( '/', $this->mAddress
);
160 if ( count( $rangeParts ) == 2 ) {
161 $this->mNetworkBits
= $rangeParts[1];
163 $this->mNetworkBits
= 32;
165 $this->mIntegerAddr
= ip2long( $rangeParts[0] );
167 $this->mNetworkBits
= false;
168 $this->mIntegerAddr
= false;
173 * Callback with a Block object for every block
174 * @return integer number of blocks;
176 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
178 global $wgAntiLockFlags;
180 $block = new Block();
181 if ( $flags & EB_FOR_UPDATE
) {
182 $db =& wfGetDB( DB_MASTER
);
183 if ( $wgAntiLockFlags & ALF_NO_BLOCK_LOCK
) {
186 $options = 'FOR UPDATE';
188 $block->forUpdate( true );
190 $db =& wfGetDB( DB_SLAVE
);
193 if ( $flags & EB_RANGE_ONLY
) {
194 $cond = " WHERE ipb_address LIKE '%/%'";
199 $ipblocks = $db->tableName( 'ipblocks' );
201 $sql = "SELECT * FROM $ipblocks $cond ORDER BY ipb_timestamp DESC $options";
202 $res = $db->query( $sql, 'Block::enumBans' );
203 $num_rows = $db->numRows( $res );
205 while ( $row = $db->fetchObject( $res ) ) {
206 $block->initFromRow( $row );
207 if ( ( $flags & EB_RANGE_ONLY
) && $block->getNetworkBits() == 32 ) {
211 if ( !( $flags & EB_KEEP_EXPIRED
) ) {
212 if ( !$block->deleteIfExpired() ) {
213 $callback( $block, $tag );
216 $callback( $block, $tag );
219 wfFreeResult( $res );
225 $fname = 'Block::delete';
229 $dbw =& wfGetDB( DB_MASTER
);
231 if ( $this->mAddress
== '' ) {
232 $condition = array( 'ipb_id' => $this->mId
);
234 $condition = array( 'ipb_address' => $this->mAddress
);
236 $dbw->delete( 'ipblocks', $condition, $fname );
242 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
243 $dbw =& wfGetDB( DB_MASTER
);
244 $ipb_id = $dbw->nextSequenceValue('ipblocks_ipb_id_val');
245 $dbw->insert( 'ipblocks',
248 'ipb_address' => $this->mAddress
,
249 'ipb_user' => $this->mUser
,
250 'ipb_by' => $this->mBy
,
251 'ipb_reason' => $this->mReason
,
252 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
253 'ipb_auto' => $this->mAuto
,
254 'ipb_expiry' => $this->mExpiry ?
255 $dbw->timestamp($this->mExpiry
) :
263 function deleteIfExpired()
265 if ( $this->isExpired() ) {
266 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
270 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
277 wfDebug( "Block::isExpired() checking current " . wfTimestampNow() . " vs $this->mExpiry\n" );
278 if ( !$this->mExpiry
) {
281 return wfTimestampNow() > $this->mExpiry
;
287 return $this->mAddress
!= '';
290 function updateTimestamp()
292 if ( $this->mAuto
) {
293 $this->mTimestamp
= wfTimestamp();
294 $this->mExpiry
= Block
::getAutoblockExpiry( $this->mTimestamp
);
296 $dbw =& wfGetDB( DB_MASTER
);
297 $dbw->update( 'ipblocks',
299 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp
),
300 'ipb_expiry' => $dbw->timestamp($this->mExpiry
),
301 ), array( /* WHERE */
302 'ipb_address' => $this->mAddress
303 ), 'Block::updateTimestamp'
310 /* private */ function clearCache()
312 global $wgBlockCache;
313 if ( is_object( $wgBlockCache ) ) {
314 $wgBlockCache->loadFromDB();
318 function getIntegerAddr()
320 return $this->mIntegerAddr
;
323 function getNetworkBits()
325 return $this->mNetworkBits
;
328 function forUpdate( $x = NULL ) {
329 return wfSetVar( $this->mForUpdate
, $x );
332 /* static */ function getAutoblockExpiry( $timestamp )
334 global $wgAutoblockExpiry;
335 return wfTimestamp( TS_MW
, wfTimestamp( TS_UNIX
, $timestamp ) +
$wgAutoblockExpiry );
338 /* static */ function normaliseRange( $range )
340 $parts = explode( '/', $range );
341 if ( count( $parts ) == 2 ) {
342 $shift = 32 - $parts[1];
343 $ipint = ip2long( $parts[0] );
344 $ipint = $ipint >> $shift << $shift;
345 $newip = long2ip( $ipint );
346 $range = "$newip/{$parts[1]}";